<html>
<head><meta charset="utf-8"><title>Iterators for mpsc::Receiver · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Iterators.20for.20mpsc.3A.3AReceiver.html">Iterators for mpsc::Receiver</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="203208778"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Iterators%20for%20mpsc%3A%3AReceiver/near/203208778" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> chabing <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Iterators.20for.20mpsc.3A.3AReceiver.html#203208778">(Jul 07 2020 at 22:00)</a>:</h4>
<p>The <a href="https://doc.rust-lang.org/std/sync/mpsc/struct.TryIter.html">TryIter</a> for mpsc::Receiver yields <code>None</code> for either of 2 conditions:</p>
<ul>
<li>
<p>The sending end of the channel disconnected<br>
<strong>OR</strong></p>
</li>
<li>
<p>The channel is empty</p>
</li>
</ul>
<p>This obscurity makes it impossible for consumers of the iterator to detect if/when the channel is disconnected, and thus impossible to properly react if the channel gets disconnected.</p>
<p>There should also be an iterator for <code>Receiver</code> whose <code>Item</code> type is <code>Result&lt;T,std::sync::mpsc::RecvError&gt;</code>, so that disconnects can be detected.</p>



<a name="203209190"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Iterators%20for%20mpsc%3A%3AReceiver/near/203209190" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Iterators.20for.20mpsc.3A.3AReceiver.html#203209190">(Jul 07 2020 at 22:04)</a>:</h4>
<p>I think you could use <code>iter::from_fn</code> to approximate this, at least</p>



<a name="203209439"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Iterators%20for%20mpsc%3A%3AReceiver/near/203209439" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> chabing <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Iterators.20for.20mpsc.3A.3AReceiver.html#203209439">(Jul 07 2020 at 22:07)</a>:</h4>
<p>Yes, there are possible workarounds. But this should be addressed in the standard library because of the double-meaning of <code>None</code></p>



<a name="203209483"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Iterators%20for%20mpsc%3A%3AReceiver/near/203209483" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Iterators.20for.20mpsc.3A.3AReceiver.html#203209483">(Jul 07 2020 at 22:07)</a>:</h4>
<p>which would you map to the iterator's <code>None</code>, empty or disconnected?</p>



<a name="203209693"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Iterators%20for%20mpsc%3A%3AReceiver/near/203209693" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> chabing <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Iterators.20for.20mpsc.3A.3AReceiver.html#203209693">(Jul 07 2020 at 22:10)</a>:</h4>
<p>Empty would go to <code>None</code>. Iterators should only use None when they are empty, not overload its meaning.</p>



<a name="203209751"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Iterators%20for%20mpsc%3A%3AReceiver/near/203209751" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> chabing <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Iterators.20for.20mpsc.3A.3AReceiver.html#203209751">(Jul 07 2020 at 22:10)</a>:</h4>
<p>Plus, <a href="https://doc.rust-lang.org/std/sync/mpsc/struct.RecvError.html">RecvError</a> already exists</p>



<a name="203209759"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Iterators%20for%20mpsc%3A%3AReceiver/near/203209759" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Iterators.20for.20mpsc.3A.3AReceiver.html#203209759">(Jul 07 2020 at 22:11)</a>:</h4>
<div class="codehilite"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">try_iter</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">(</span><span class="n">receiver</span>: <span class="kp">&amp;</span><span class="nc">Receiver</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">impl</span><span class="w"> </span><span class="nb">Iterator</span><span class="o">&lt;</span><span class="n">Item</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Result</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">TryRecvError</span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nb">&#39;_</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">std</span>::<span class="n">iter</span>::<span class="n">from_fn</span><span class="p">(</span><span class="k">move</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="k">match</span><span class="w"> </span><span class="n">receiver</span><span class="p">.</span><span class="n">try_recv</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nb">Err</span><span class="p">(</span><span class="n">TryRecvError</span>::<span class="n">Empty</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="nb">None</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="n">other</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">other</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="p">})</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="203209805"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Iterators%20for%20mpsc%3A%3AReceiver/near/203209805" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Iterators.20for.20mpsc.3A.3AReceiver.html#203209805">(Jul 07 2020 at 22:11)</a>:</h4>
<p>oh, I didn't map to <code>RecvError</code>, but that's easy enough</p>



<a name="203209946"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Iterators%20for%20mpsc%3A%3AReceiver/near/203209946" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> chabing <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Iterators.20for.20mpsc.3A.3AReceiver.html#203209946">(Jul 07 2020 at 22:13)</a>:</h4>
<p>I know it can be worked around, but my point is that the current try_recv is flawed.</p>



<a name="203209957"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Iterators%20for%20mpsc%3A%3AReceiver/near/203209957" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> chabing <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Iterators.20for.20mpsc.3A.3AReceiver.html#203209957">(Jul 07 2020 at 22:13)</a>:</h4>
<p>One sec, I'll make an example</p>



<a name="203209971"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Iterators%20for%20mpsc%3A%3AReceiver/near/203209971" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Iterators.20for.20mpsc.3A.3AReceiver.html#203209971">(Jul 07 2020 at 22:14)</a>:</h4>
<p><code>mpsc</code> is considered flawed in general...</p>



<a name="203210021"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Iterators%20for%20mpsc%3A%3AReceiver/near/203210021" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Iterators.20for.20mpsc.3A.3AReceiver.html#203210021">(Jul 07 2020 at 22:14)</a>:</h4>
<p>maybe this should be proposed for <code>crossbeam-channel</code></p>



<a name="203210027"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Iterators%20for%20mpsc%3A%3AReceiver/near/203210027" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> chabing <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Iterators.20for.20mpsc.3A.3AReceiver.html#203210027">(Jul 07 2020 at 22:14)</a>:</h4>
<p>Oh, how is it generally flawed? Should I be using something else?</p>



<a name="203210057"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Iterators%20for%20mpsc%3A%3AReceiver/near/203210057" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Iterators.20for.20mpsc.3A.3AReceiver.html#203210057">(Jul 07 2020 at 22:14)</a>:</h4>
<p>there's some background here: <a href="https://stjepang.github.io/2019/03/02/new-channels.html">https://stjepang.github.io/2019/03/02/new-channels.html</a></p>



<a name="203210258"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Iterators%20for%20mpsc%3A%3AReceiver/near/203210258" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Iterators.20for.20mpsc.3A.3AReceiver.html#203210258">(Jul 07 2020 at 22:17)</a>:</h4>
<p>Some links embedded from there:<br>
regrets: <a href="https://github.com/rust-lang/rust/pull/42397#issuecomment-315867774">https://github.com/rust-lang/rust/pull/42397#issuecomment-315867774</a><br>
known panic: <a href="https://github.com/rust-lang/rust/issues/39364">https://github.com/rust-lang/rust/issues/39364</a></p>



<a name="203210369"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Iterators%20for%20mpsc%3A%3AReceiver/near/203210369" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Iterators.20for.20mpsc.3A.3AReceiver.html#203210369">(Jul 07 2020 at 22:18)</a>:</h4>
<p>and also this topic: <a href="#narrow/stream/219381-t-libs/topic/Replacing.20mpsc/near/197012164">https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/Replacing.20mpsc/near/197012164</a></p>



<a name="203210623"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Iterators%20for%20mpsc%3A%3AReceiver/near/203210623" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> chabing <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Iterators.20for.20mpsc.3A.3AReceiver.html#203210623">(Jul 07 2020 at 22:21)</a>:</h4>
<p>What should I do then? I need this functionality for what I'm doing</p>



<a name="203210701"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Iterators%20for%20mpsc%3A%3AReceiver/near/203210701" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> chabing <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Iterators.20for.20mpsc.3A.3AReceiver.html#203210701">(Jul 07 2020 at 22:22)</a>:</h4>
<p>Should I use some external crate or just do the above workaround?</p>



<a name="203210996"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Iterators%20for%20mpsc%3A%3AReceiver/near/203210996" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Iterators.20for.20mpsc.3A.3AReceiver.html#203210996">(Jul 07 2020 at 22:25)</a>:</h4>
<p><code>crossbeam-channel</code> is a pretty safe bet. <code>flume</code> is a newer alternative.</p>



<a name="203211083"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Iterators%20for%20mpsc%3A%3AReceiver/near/203211083" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Iterators.20for.20mpsc.3A.3AReceiver.html#203211083">(Jul 07 2020 at 22:26)</a>:</h4>
<p>regardless, I don't think any of them have the exact iterator you want</p>



<a name="203211129"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Iterators%20for%20mpsc%3A%3AReceiver/near/203211129" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Iterators.20for.20mpsc.3A.3AReceiver.html#203211129">(Jul 07 2020 at 22:27)</a>:</h4>
<p>but it may be faster to get it added to those external crates, if only to avoid <code>std</code> stabilization delays</p>



<a name="203211146"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Iterators%20for%20mpsc%3A%3AReceiver/near/203211146" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> chabing <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Iterators.20for.20mpsc.3A.3AReceiver.html#203211146">(Jul 07 2020 at 22:28)</a>:</h4>
<p>Ok.</p>



<a name="203211184"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Iterators%20for%20mpsc%3A%3AReceiver/near/203211184" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> chabing <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Iterators.20for.20mpsc.3A.3AReceiver.html#203211184">(Jul 07 2020 at 22:28)</a>:</h4>
<p>Thanks for the help</p>



<a name="203211217"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Iterators%20for%20mpsc%3A%3AReceiver/near/203211217" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> chabing <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Iterators.20for.20mpsc.3A.3AReceiver.html#203211217">(Jul 07 2020 at 22:28)</a>:</h4>
<p>Oh, is there an ongoing discussion about deprecating or replacing mpsc withing std?</p>



<a name="203211419"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Iterators%20for%20mpsc%3A%3AReceiver/near/203211419" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Iterators.20for.20mpsc.3A.3AReceiver.html#203211419">(Jul 07 2020 at 22:31)</a>:</h4>
<p>there was, but I'm not sure of the current opinion</p>



<a name="203242404"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Iterators%20for%20mpsc%3A%3AReceiver/near/203242404" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Iterators.20for.20mpsc.3A.3AReceiver.html#203242404">(Jul 08 2020 at 08:21)</a>:</h4>
<p><span class="user-mention" data-user-id="247690">@chabing</span> You can read <a href="#narrow/stream/219381-t-libs/topic/Replacing.20mpsc">https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/Replacing.20mpsc</a> for the most recent discussion.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>